package me.zd.tool.hardlink.view.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.function.Consumer;

public class CountDownButtonListener implements ActionListener {

    private final Consumer<Integer> countDown;
    private final Runnable r;
    private final int fullCount;
    private int counter;

    public CountDownButtonListener(int fullCount, Consumer<Integer> countDown, Runnable r) {
        this.countDown = countDown;
        this.r = r;
        this.fullCount = fullCount;
        resetCounter();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (--counter > 0) {
            countDown.accept(counter);
        } else {
            r.run();
            resetCounter();
        }
    }

    private void resetCounter() {
        counter = fullCount;
    }

}
